package Thread;

/**
 * @author: lqh
 * @description: 两个人AB通过一个账户A在柜台取钱和B在ATM机取钱！
 * @program: Demo
 * @create: 2018-04-13 09:43
 **/
public class GetMoney implements Runnable{

    public String people;
    public static String account = "account";
    public static double money = 100;

    Thread thread = null;

    public GetMoney(String people) {
        this.people = people;
    }

    public void start(){
        if(thread == null){
            thread = new Thread(this,people);
        }
        thread.start();
    }

    @Override
    public void run() {
        while (money>0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (account){
                if (money>0){
                    money--;
                    System.out.println("people:"+people+"成功取出一块钱，当前余额："+money);
                }else {
                    System.out.println("余额不足...");
                }
            }
        }
    }

    public static void main(String[] args) {
        GetMoney getMoney = new GetMoney("A");
        GetMoney getMoneyB = new GetMoney("B");
        getMoney.start();
        getMoneyB.start();
    }
}
